#include "Graph.h"
#include "Search.h"
#include "Mouse.h"
#include<graphics.h>
#include<easyx.h>
#include<iostream>
using namespace std;
class Mouse;
class Search;
Graph::Graph()
{
}

void Graph::init()//Draw a 20*20 checkerboard
{
	initgraph(1080, 480, SHOWCONSOLE);//Initializes the graphics environment
	setbkcolor(WHITE);//Sets the background color of the current drawing
	cleardevice();// Clear the screen
	setorigin(120, 40);// Set the origin
	setlinecolor(BLACK);	// Sets the current line color
	putpixel(0, 0, BLACK);// Draw the point
	rectangle(0, 0, 400, 400);// Draw a rectangule
	for (float i = 0; i <= 400; i = i + 20)//Draw a square of 400 by 400 squares of 20 by 20 squares
	{
		line(i, 0, i, 400);
		line(0, i, 400, i);
	}

	setfillcolor(BLACK);// Set the current fill color
	setfillstyle(0);// set the current fill style
	char s = '0'; char s1 = '1'; char s2 = '2';//Set the digital of x and y coordinates
	settextcolor(BLACK);// set the current text color
	outtextxy(-10, -20, s);// output the character at the specified location

	outtextxy(-20, 190, s1);
	outtextxy(-10, 190, s);

	outtextxy(190, -20, s1);
	outtextxy(200, -20, s);

	outtextxy(-20, 390, s2);
	outtextxy(-10, 390, s);

	outtextxy(390, -20, s2);
	outtextxy(400, -20, s);
}

void Graph::init_map(Search s)//Draw the obstacles
{
	for (int i = 0; i < 20; i++)
	{
		for (int j = 0; j < 20; j++)
		{
			if (s.mapp[i][j] == 1)
			{
				fillrectangle(j * 20, i * 20, (j + 1) * 20, (i + 1) * 20);// draw a filled rectangle (with border)
			}
		}
	}
}

void Graph::erase_map()//Erase the map
{
	setfillcolor(WHITE);
	setfillstyle(0);
	for (int i = 0; i < 20; i++)//Fill all rectangles with white
	{
		for (int j = 0; j < 20; j++)
		{
			fillrectangle(j * 20, i * 20, (j + 1) * 20, (i + 1) * 20);//i->y j->x
		}
	}
	setfillcolor(BLACK);
}

int Graph::init_points_to_solve(int mode,Mouse mouse)//Set the starting and final point
{
	int x = mouse.mapjudge() / 100; //The x coordinate of the small rectangle where the mouse is pointing to
	int y = mouse.mapjudge() % 100; // The y coordinate of the small rectangle where the mouse is pointing to
	
	if (mode == DONE)//Set the starting and final point automatically
	{
		x = 1; y = 1;
		sx = x - 1; sy = y - 1;
		setfillcolor(GREEN);
		fillrectangle(0, 0, 20, 20); //Set (0,0) as the starting point
		x = 20; y = 20;
		ex = x - 1; ey = y - 1;
		setfillcolor(RED);
		fillrectangle(19 * 20, 19 * 20, 20 * 20, 20 * 20); //Set (19,19) as the final point
		return 1;
	}
	else if (mode == START)//Set the starting point by yourself
	{
		setfillcolor(GREEN);
		cout << x << " " << y << endl; //Output origin coordinate
		sx = x-1; sy = y-1;
		fillrectangle((x-1) * 20, (y-1) * 20, x  * 20, y  * 20);
		return sx * 100 + sy;
	}
	else if(mode==END)//Set the final point by yourself
	{
		setfillcolor(RED);
		cout << x << " " << y << endl;
		ex = x-1; ey = y-1;
		fillrectangle((x - 1) * 20, (y - 1) * 20, x * 20, y * 20);
		return ex * 100 + ey;
	}
	
	setfillcolor(BLACK);
}

void Graph::show_ans(Search s)// draw the shortest path
{
	/*cout << "ans.size():" << s.ans.size() << endl;
	for (size_t i = 0; i < s.ans.size(); i++)
	{
		cout << "i:"<<i<<" " << s.ans[i].y+1 << " " << s.ans[i].x +1<<" "<< s.ans[i].pre<<endl;
	}*/
	for (size_t i = 0; i < s.ans.size()-1; i++)
	{
		line( 10 + s.ans[i].y * 20, 10 + s.ans[i].x * 20,  10 + s.ans[i + 1].y * 20,  10 + s.ans[i + 1].x * 20);
	}
	
}


Graph::~Graph()
{
}
